package xerr

// 通用状态码
const (
	OK              uint32 = 200
	ERROR           uint32 = 500
	INVALID_PARAM   uint32 = 400
	UNAUTHORIZED    uint32 = 401
	FORBIDDEN       uint32 = 403
	NOT_FOUND       uint32 = 404
	REQUEST_TIMEOUT uint32 = 408
	SERVER_BUSY     uint32 = 503
)

// 自定义业务错误码
const (
	// 用户模块错误码 1000~1999
	UserAlreadyExists  uint32 = 1001
	UserNotFound       uint32 = 1002
	PasswordError      uint32 = 1003
	TokenGenerateError uint32 = 1004
	TokenExpired       uint32 = 1005
	TokenInvalid       uint32 = 1006

	// 帖子模块错误码 2000~2999
	PostNotFound     uint32 = 2001
	PostCreateFailed uint32 = 2002
	PostUpdateFailed uint32 = 2003
	PostDeleteFailed uint32 = 2004

	// 评论模块错误码 3000~3999
	CommentNotFound     uint32 = 3001
	CommentCreateFailed uint32 = 3002
	CommentDeleteFailed uint32 = 3003
)

// 错误码映射表
var msgMap = map[uint32]string{
	OK:              "成功",
	ERROR:           "服务器内部错误",
	INVALID_PARAM:   "参数错误",
	UNAUTHORIZED:    "未授权",
	FORBIDDEN:       "禁止访问",
	NOT_FOUND:       "资源不存在",
	REQUEST_TIMEOUT: "请求超时",
	SERVER_BUSY:     "服务器繁忙，请稍后再试",

	// 用户模块错误码
	UserAlreadyExists:  "用户已存在",
	UserNotFound:       "用户不存在",
	PasswordError:      "密码错误",
	TokenGenerateError: "生成令牌失败",
	TokenExpired:       "令牌已过期",
	TokenInvalid:       "无效的令牌",

	// 帖子模块错误码
	PostNotFound:     "帖子不存在",
	PostCreateFailed: "创建帖子失败",
	PostUpdateFailed: "更新帖子失败",
	PostDeleteFailed: "删除帖子失败",

	// 评论模块错误码
	CommentNotFound:     "评论不存在",
	CommentCreateFailed: "创建评论失败",
	CommentDeleteFailed: "删除评论失败",
}

// 获取错误码对应的错误信息
func MapErrMsg(errCode uint32) string {
	if msg, ok := msgMap[errCode]; ok {
		return msg
	}
	return "服务器开小差了，请稍后再试"
}
